package yxy.game.pm2.active.command.monthfund;

import yxy.apple.logger.Logger;
import yxy.cherry.data.bean.actmonthfund;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_超值月基金;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.ActiveMessage.ActMonthFundJackpotRequest;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.Map;

@MessageType(proto = Command.ActMonthFundJackpot, classtype = ActiveMessage.ActMonthFundJackpotRequest.class)
public class CMD_Active_超值月基金_Jackpot extends Command<ActMonthFundJackpotRequest> {

	private static final Logger logger = Logger.create(CMD_Active_超值月基金_Jackpot.class);
	
	@Override
	public void execute(MessageSource messageSource, ActMonthFundJackpotRequest request) throws Exception {
		String playercode = messageSource.getTag();
		int type = request.getType();
		ActiveMessage.ActMonthFundJackpotResponse.Builder response = ActiveMessage.ActMonthFundJackpotResponse.newBuilder();
		response.setType(type);
		
		ActiveModule_超值月基金 am = ActiveModule_超值月基金.get(playercode,false);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		if(!am.running()) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		if(type != ActiveModule_超值月基金.mftype1 && type != ActiveModule_超值月基金.mftype2) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		Map<String, actmonthfund> datamap = ActDataCenter.getAllData(actmonthfund.class, playercode);
		if(datamap == null || datamap.size() == 0) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		for(actmonthfund data : datamap.values()) {
			if(data.getType() != type) {
				continue;
			}
			
			ActiveMessage.MonthFundJackpot.Builder build = ActiveMessage.MonthFundJackpot.newBuilder();
			build.setDay(data.getDay());
			build.setId(data.getId());
			build.setShow(data.getShow());
			
			Currency cur = Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());
			PackageMessage.Item item = new CurrencyHandle().packItem(cur);
			build.setCur(item);
			
			response.addJackpot(build.build());
		}
		
		send(messageSource, response, Result.Success);
	}
	
	private void send(MessageSource messageSource, ActiveMessage.ActMonthFundJackpotResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}

}
